﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Manage.Model;
using Newtonsoft.Json;

namespace Manage.Basic
{
    public partial class FrmCompanyMgr : Form
    {
        public FrmCompanyMgr()
        {
            InitializeComponent();
        }
        private int Sid;
        private string SCname;
        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            Sid = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["id"]));
            SCname = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["公司名称"]).ToString();
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                ManagerCompany mc = new ManagerCompany();
                mc.CName = tbxCompName.Text.Trim();
                RetModel ret = Share.PostData("51API/Company/CompanyAdd", dc, JsonConvert.SerializeObject(mc));
                if (ret.Success)
                {
                    MessageBox.Show("添加成功!");
                    barButtonItem4_ItemClick(sender, e);
                    tbxCompName.Text = "";
                }
                else
                {
                    MessageBox.Show("添加失败，未知错误!");
                    barButtonItem4_ItemClick(sender, e);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                ManagerCompany mc = new ManagerCompany();
                mc.id = Sid;
                RetModel ret = Share.PostData("51API/Company/CompanyDel", dc, JsonConvert.SerializeObject(mc));
                if (ret.Success)
                {
                    MessageBox.Show("删除成功!");
                    barButtonItem4_ItemClick(sender, e);
                    tbxCompName.Text = "";
                }
                else
                {
                    MessageBox.Show("删除失败，未知错误！");
                    barButtonItem4_ItemClick(sender, e);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改  
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                ManagerCompany mc = new ManagerCompany();
                mc.id = Sid;
                mc.CName = tbxCompName.Text.Trim();
                RetModel ret = Share.PostData("51API/Company/CompanyEdit", dc, JsonConvert.SerializeObject(mc));
                if (ret.Success)
                {
                    MessageBox.Show("修改成功！");
                    barButtonItem4_ItemClick(sender, e);
                    tbxCompName.Text = "";
                }
                else
                {
                    MessageBox.Show("修改失败，未知错误！");
                    barButtonItem4_ItemClick(sender, e);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxCompName.Text))
            {
                gridControl1.DataSource = null;
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                RetModel ret = Share.PostData("51API/Company/GetCompanyList", dc, null);
                if(ret.Success)
                {
                    
                    ManagerCompany[] mc = JsonConvert.DeserializeObject<ManagerCompany[]>(ret.Data.ToString());
                    if (mc == null)
                    {
                        MessageBox.Show("未找到相关信息！");
                        gridControl1.DataSource = null;
                    }
                    else
                    {
                        DataTable dt = new DataTable();
                        dt.Columns.Add("id");
                        dt.Columns.Add("公司名称");
                        for (int i = 0; i < mc.Length; i++)
                        {
                            DataRow dr = dt.NewRow();
                            dr["id"] = mc[i].id;
                            dr["公司名称"] = mc[i].CName;
                            dt.Rows.Add(dr);
                        }
                        gridControl1.DataSource = dt;
                        gridView1.Columns[0].Visible = false;
                    }
                }
                else
                {
                    gridControl1.DataSource = null;
                    MessageBox.Show("查询失败，未知错误！");
                    
                }

            }
            else
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                ManagerCompany mc = new ManagerCompany();
                mc.CName = tbxCompName.Text.Trim();
                RetModel ret = Share.PostData("51API/Company/GetCompanyByName", dc, JsonConvert.SerializeObject(mc));
                if (ret.Success)
                {
                    ManagerCompany[] Rmc = JsonConvert.DeserializeObject<ManagerCompany[]>(ret.Data.ToString());
                    if (mc == null)
                    {
                        MessageBox.Show("未找到相关信息！");
                        gridControl1.DataSource = null;
                    }
                    else
                    {
                        DataTable dt = new DataTable();
                        dt.Columns.Add("id");
                        dt.Columns.Add("公司名称");
                        for (int i = 0; i < Rmc.Length; i++)
                        {
                            DataRow dr = dt.NewRow();
                            dr["id"] = Rmc[i].id;
                            dr["公司名称"] = Rmc[i].CName;
                            dt.Rows.Add(dr);
                        }
                        gridControl1.DataSource = dt;
                        gridView1.Columns[0].Visible = false;
                    }
                }
                else
                {
                    gridControl1.DataSource = null;
                    MessageBox.Show("查询失败，未知错误！");
                }
            }
        }
        /// <summary>
        /// 载入窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmCompanyMgr_Load(object sender, EventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
